﻿using System;
using System.Collections.Generic;
using System.IO;
using Labthink.PCSoft.UniversalHost4.Model;
using Labthink.PCSoft.UniversalHost4.Utility;
using NLog;

namespace Labthink.PCSoft.UniversalHost4.DataAccess
{
    public class BaseRepository
    {
        private static readonly string Dir = AppDomain.CurrentDomain.BaseDirectory;
        protected static readonly string DbFile = Path.Combine(Dir, "Config\\App.db");//主数据路径
        protected static readonly string OperationLogDb = Path.Combine(Dir, "Config\\OperationLogs.db");//操作日志
        protected static readonly string TempDir = Path.Combine(Dir, "Temp");//临时文件存放位置
        protected static readonly string IniFile = Path.Combine(Dir, "Config\\AppConfig.ini");//主配置文件
        protected static readonly string DataFilePath = Path.Combine(Dir, "Db");//试验数据存储路径

        protected static string Type;
        protected static string Device;
        protected static string Year;
        protected static string Sn;

        public static List<FullData> SelfQuery;

        static BaseRepository()
        {
            if (!Directory.Exists(DataFilePath))
            {
                Directory.CreateDirectory(DataFilePath);
            }
        }

        /// <summary>
        /// 判断数据库文件是否存在
        /// </summary>
        protected static void IsDbExist()
        {
            if (!File.Exists(DbFile))
            {
                throw new FileNotFoundException("Db file is not exists");
            }

            if (!File.Exists(OperationLogDb))
            {
                throw new FileNotFoundException("Db file is not exists");
            }
        }

        public static void GetDeviceId()
        {
            Type = IniHelper.GetIni(IniFile, "base", "type", "00").PadLeft(2, '0');
            Device = IniHelper.GetIni(IniFile, "base", "device", "00").PadLeft(2, '0');
            Year = IniHelper.GetIni(IniFile, "base", "year", "2017").PadLeft(4, '0');
            Sn = IniHelper.GetIni(IniFile, "base", "sn", "1").PadLeft(3, '0');
        }
    }
}
